//
//  GPCustomSegment.m
//  SegmentView
//
//  Created by GuoPeng on 16/4/20.
//  Copyright © 2016年 GuoPeng. All rights reserved.
//

#import "GPCustomSegment.h"
#import <Masonry/Masonry.h>
#import "MacroDefinition.h"
#import <ReactiveCocoa.h>
#import "GPCustomMD.h"


@interface GPCustomSegment ()

@property (weak, nonatomic) IBOutlet UIImageView *image1;
@property (weak, nonatomic) IBOutlet UIImageView *image2;
@property (weak, nonatomic) IBOutlet UIImageView *image3;
@property (weak, nonatomic) IBOutlet UILabel *label1;
@property (weak, nonatomic) IBOutlet UILabel *label2;
@property (weak, nonatomic) IBOutlet UILabel *label3;
@property (weak, nonatomic) IBOutlet UIView *selectView;

@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) NSArray *imageArray;
@property (nonatomic, strong) NSArray *selectImageArray;


@property (nonatomic, strong) NSArray *labelArray;
@property (nonatomic, strong) NSArray *defaultImageArray;


@property (weak, nonatomic) IBOutlet UIButton *button1;
@property (weak, nonatomic) IBOutlet UIButton *button2;
@property (weak, nonatomic) IBOutlet UIButton *button3;

@end

@implementation GPCustomSegment


- (instancetype)initWithFrame:(CGRect)frame imageArray:(nonnull NSArray<NSString *> *)imageArray selectImageArray:(nonnull NSArray<NSString *> *)selectImageArray titleArray:(nonnull NSArray<NSString *> *)titleArray defaultSelectIndex:(NSInteger)selectedIndex selectBlock:(SelectIndexBlock)selectBlock
{
    self = [[[NSBundle mainBundle] loadNibNamed:@"GPCustomSegment" owner:self options:nil] objectAtIndex:0];
    if (self) {
        self.frame = frame;
        NSAssert([imageArray count] == 3 && [titleArray count] == 3 && [selectImageArray count] == 3, @"Parameters error");
        self.selectBlock = selectBlock;
        self.titleArray = titleArray;
        self.imageArray = imageArray;
        self.selectImageArray = selectImageArray;
        self.selectedIndex = selectedIndex;
        
        [self initViews];
    }
    return  self;
}

- (void)selectedIndex:(NSInteger)selectIndex
{
    NSAssert(selectIndex >= 0 && selectIndex <= 2, @"Parameters error");

    [self selectIndex:selectIndex];
}


- (void)initViews
{
    self.label1.text = self.titleArray[0];
    self.label2.text = self.titleArray[1];
    self.label3.text = self.titleArray[2];
    
    self.image1.image = [UIImage imageNamed:self.imageArray[0]];
    self.image2.image = [UIImage imageNamed:self.imageArray[1]];
    self.image3.image = [UIImage imageNamed:self.imageArray[2]];
    
    self.defaultImageArray = [NSArray arrayWithObjects:self.image1,self.image2,self.image3, nil];
    self.labelArray = [NSArray arrayWithObjects:self.label1,self.label2,self.label3, nil];
    
    self.selectView.backgroundColor = RGB(135, 244, 224);
    
    self.image1.image = [UIImage imageNamed:self.selectImageArray[0]] ;
}

- (void)selectIndex:(NSInteger)selectIndex
{
    for (UILabel *label in self.labelArray) {
        label.textColor = RGB(158, 155, 156);
    }
    
    self.image1.image = [UIImage imageNamed:self.imageArray[0]];
    self.image2.image = [UIImage imageNamed:self.imageArray[1]];
    self.image3.image = [UIImage imageNamed:self.imageArray[2]];
    
    switch (selectIndex) {
        case 0:
        {
            _label1.textColor = RGB(135, 244, 224);
            self.image1.image = [UIImage imageNamed:self.selectImageArray[0]];
            
            WS
            [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
                SS
                strongSelf.selectView.center = CGPointMake(strongSelf.button1.center.x, strongSelf.selectView.center.y);
            } completion:nil];
            
        }
            break;
            
        case 1:
        {
            _label2.textColor = RGB(135, 244, 224);
            self.image2.image = [UIImage imageNamed:self.selectImageArray[1]];
            
            WS
            [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
                SS
                strongSelf.selectView.center = CGPointMake(strongSelf.button2.center.x, strongSelf.selectView.center.y);
            } completion:nil];
//            [UIView animateWithDuration:0.5 animations:^{
//                self.selectView.center = CGPointMake(self.button2.center.x, self.selectView.center.y);
//            } completion:^(BOOL finished) {
//                
//            }];
            
        }
            break;
            
        case 2:
        {
            _label3.textColor = RGB(135, 244, 224);
            self.image3.image = [UIImage imageNamed:self.selectImageArray[2]];
            
            
            WS
            [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
                SS
                strongSelf.selectView.center = CGPointMake(strongSelf.button3.center.x, strongSelf.selectView.center.y);
            } completion:nil];


        }
            break;
            
        default:
            break;
    }
}

- (IBAction)select:(UIButton *)sender
{
    switch (sender.tag) {
        case 111:
        {
            [self selectIndex:0];
            self.selectBlock(0);
            
        }
            break;
        
        case 222:
        {
            [self selectIndex:1];
            self.selectBlock(1);
        }
            break;
            
        case 333:
        {
            [self selectIndex:2];
            self.selectBlock(2);
        }
            break;
            
        default:
            break;
    }
    
}






/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
